package com.calvin.study.utils;

import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.calvin.study.utils.ThreadUtil.CustomThreadFactory;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CpuIntenseTargetThreadPoolLazyHolder {
    //线程池： 用于CPU密集型任务
    private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
    		ThreadUtil.MAXIMUM_POOL_SIZE,
    		ThreadUtil.MAXIMUM_POOL_SIZE,
    		ThreadUtil.KEEP_ALIVE_SECONDS,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue(ThreadUtil.QUEUE_SIZE),
            new CustomThreadFactory("cpu"));


    public static ThreadPoolExecutor getInnerExecutor() {
        return EXECUTOR;
    }

    static {
        log.info("线程池已经初始化");

        EXECUTOR.allowCoreThreadTimeOut(true);
        //JVM关闭时的钩子函数
        Runtime.getRuntime().addShutdownHook(
                new ShutdownHookThread("CPU密集型任务线程池", new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        //优雅关闭线程池
                    	ThreadUtil.shutdownThreadPoolGracefully(EXECUTOR);
                        return null;
                    }
                }));
    }
}
